﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Practices.EnterpriseLibrary.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;
using System.Data.SqlClient;

namespace TB.DALSqlProvider
{
    /// <summary>
    /// 对*MpRespMsgUser*操作数据库(针对SQL Server)的实现
    /// </summary>
    public class MpRespMsgUserSqlDALProvider:IDAL.IDALMpRespMsgUser
    { 
        public int Insert(TB.Entity.MpRespMsgUser obj)
        {
           //1.通过database工厂得到当前数据库连接的对象  db
            Database db = DatabaseFactory.CreateDatabase();
            //2.通过db 获得一个sp构成的cmd
            var cmd = db.GetStoredProcCommand("proc_MpRespMsgUser_Insert") as SqlCommand;
            //3.cmd参数赋值
            //cmd.Parameters.AddWithValue("@id_auto",(obj.id_auto==0||obj.id_auto==null)?DBNull.Value:(object)obj.id_auto);
            cmd.Parameters.AddWithValue("@toUserName",(obj.toUserName==String.Empty||obj.toUserName==null)?DBNull.Value:(object)obj.toUserName);
            cmd.Parameters.AddWithValue("@fromUserName",(obj.fromUserName==String.Empty||obj.fromUserName==null)?DBNull.Value:(object)obj.fromUserName);
            cmd.Parameters.AddWithValue("@respmsg_id_auto",(obj.respmsg_id_auto==0)?DBNull.Value:(object)obj.respmsg_id_auto);
            cmd.Parameters.AddWithValue("@realFromUserName",(obj.realFromUserName==String.Empty||obj.realFromUserName==null)?DBNull.Value:(object)obj.realFromUserName);
            cmd.Parameters.AddWithValue("@realToUserName",(obj.realToUserName==String.Empty||obj.realToUserName==null)?DBNull.Value:(object)obj.realToUserName);
            cmd.Parameters.AddWithValue("@createTime",(obj.createTime==String.Empty||obj.createTime==null)?DBNull.Value:(object)obj.createTime);
            //4.重要:通过db来执行这个cmd (在db内部对cmd进行构造)
           return db.ExecuteNonQuery(cmd);
        }
        
        public int Update(TB.Entity.MpRespMsgUser obj)
        {
            //1.通过database工厂得到当前数据库连接的对象  db
            Database db = DatabaseFactory.CreateDatabase();
            //2.通过db 获得一个sp构成的cmd
            var cmd = db.GetStoredProcCommand("proc_MpRespMsgUser_Update") as SqlCommand;
            //3.cmd参数赋值
            //cmd.Parameters.AddWithValue("@id_auto",(obj.id_auto==0||obj.id_auto==null)?DBNull.Value:(object)obj.id_auto);
            cmd.Parameters.AddWithValue("@toUserName",(obj.toUserName==String.Empty||obj.toUserName==null)?DBNull.Value:(object)obj.toUserName);
            cmd.Parameters.AddWithValue("@fromUserName",(obj.fromUserName==String.Empty||obj.fromUserName==null)?DBNull.Value:(object)obj.fromUserName);
            cmd.Parameters.AddWithValue("@respmsg_id_auto",(obj.respmsg_id_auto==0)?DBNull.Value:(object)obj.respmsg_id_auto);
            cmd.Parameters.AddWithValue("@realFromUserName",(obj.realFromUserName==String.Empty||obj.realFromUserName==null)?DBNull.Value:(object)obj.realFromUserName);
            cmd.Parameters.AddWithValue("@realToUserName",(obj.realToUserName==String.Empty||obj.realToUserName==null)?DBNull.Value:(object)obj.realToUserName);
            cmd.Parameters.AddWithValue("@createTime",(obj.createTime==String.Empty||obj.createTime==null)?DBNull.Value:(object)obj.createTime);
            //4.重要:通过db来执行这个cmd (在db内部对cmd进行构造)
            return db.ExecuteNonQuery(cmd);
        }
        
        public int Delete(TB.Entity.MpRespMsgUser obj)
        {
            //1.通过database工厂得到当前数据库连接的对象  db
            Database db = DatabaseFactory.CreateDatabase();
            //2.通过db 获得一个sp构成的cmd
            var cmd = db.GetStoredProcCommand("proc_MpRespMsgUser_Delete") as SqlCommand;
            //3.cmd参数赋值
           cmd.Parameters.AddWithValue("@id_auto", obj.id_auto);
            //4.重要:通过db来执行这个cmd (在db内部对cmd进行构造)
            return db.ExecuteNonQuery(cmd);
        }
        
        public List<TB.Entity.MpRespMsgUser> LoadAll()
        {
            //1.通过database工厂得到当前数据库连接的对象  db
            Database db = DatabaseFactory.CreateDatabase();
            //2.通过db 获得一个sp构成的cmd
            var cmd = db.GetStoredProcCommand("proc_MpRespMsgUser_LoadAll") as SqlCommand;
            //3.通过db执行一个DBReader
            var reader =  db.ExecuteReader(cmd);
            //4.遍历DBReader,生成新的Entity对象,然后添加到List
            List<TB.Entity.MpRespMsgUser> list = new List<TB.Entity.MpRespMsgUser>();
            while (reader.Read())
            {
               list.Add(new TB.Entity.MpRespMsgUser()
               {
                    id_auto=reader["id_auto"]==DBNull.Value?0:(int)reader["id_auto"],
                    toUserName=reader["toUserName"]==DBNull.Value?String.Empty:(string)reader["toUserName"],
                    fromUserName=reader["fromUserName"]==DBNull.Value?String.Empty:(string)reader["fromUserName"],
                    respmsg_id_auto=reader["respmsg_id_auto"]==DBNull.Value?0:(int)reader["respmsg_id_auto"],
                    realFromUserName=reader["realFromUserName"]==DBNull.Value?String.Empty:(string)reader["realFromUserName"],
                    realToUserName=reader["realToUserName"]==DBNull.Value?String.Empty:(string)reader["realToUserName"],
                    createTime=reader["createTime"]==DBNull.Value?String.Empty:(string)reader["createTime"],
               });
           }
           return list;
        }
        
        

        public TB.Entity.MpRespMsgUser LoadById(int id_auto)
        {
            //1.通过database工厂得到当前数据库连接的对象  db
            Database db = DatabaseFactory.CreateDatabase();
            //2.通过db 获得一个sp构成的cmd
            var cmd = db.GetStoredProcCommand("proc_MpRespMsgUser_LoadById") as SqlCommand;
            //3.通过db执行一个DBReader
            cmd.Parameters.AddWithValue("@id_auto", id_auto);
            var reader = db.ExecuteReader(cmd);
            //4.遍历DBReader,生成一个新的Entity对象 
            TB.Entity.MpRespMsgUser entity = null;
            while (reader.Read())
            {
                entity = new TB.Entity.MpRespMsgUser()
                {
                    id_auto=reader["id_auto"]==DBNull.Value?0:(int)reader["id_auto"],
                    toUserName=reader["toUserName"]==DBNull.Value?String.Empty:(string)reader["toUserName"],
                    fromUserName=reader["fromUserName"]==DBNull.Value?String.Empty:(string)reader["fromUserName"],
                    respmsg_id_auto=reader["respmsg_id_auto"]==DBNull.Value?0:(int)reader["respmsg_id_auto"],
                    realFromUserName=reader["realFromUserName"]==DBNull.Value?String.Empty:(string)reader["realFromUserName"],
                    realToUserName=reader["realToUserName"]==DBNull.Value?String.Empty:(string)reader["realToUserName"],
                    createTime=reader["createTime"]==DBNull.Value?String.Empty:(string)reader["createTime"],
                };
            }
            return entity;
        }
    }
}